package com.zjw.service.impl;

import com.zjw.dao.StudentDao;
import com.zjw.dao.impl.StudentDaoImpl;
import com.zjw.entity.Student;
import com.zjw.service.StudentService;
import com.zjw.utils.ConnectionUtil;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @projectName: studentCourseSelectionManagement
 * @package: com.zjw.service.impl
 * @className: StudentServiceImpl
 * @author: 翟
 * @date: 2025/4/6 21:24
 */

public class StudentServiceImpl implements StudentService {


    @Override
    public boolean addStudent(Student student) {
        Connection conn = null;
        int num = 0;
        try {
            conn = ConnectionUtil.getConnection();
            StudentDao studentDao = new StudentDaoImpl(conn);
            num = studentDao.insertStudent(student);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionUtil.closeALL(null, null, conn);
        }
        return num == 1;
    }

    @Override
    public boolean isExistStudentId(int id) {
        Connection conn = null;
        int count = 0;
        try {
            conn = ConnectionUtil.getConnection();
            StudentDao studentDao = new StudentDaoImpl(conn);
            count = studentDao.CountStudentByStudentNo(id);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionUtil.closeALL(null, null, conn);
        }
        return count == 1;
    }

    @Override
    public int getGradeIdByStudentNo(int studentNo) {
        Connection conn = null;
        int gradeId = 0;
        try {
            conn = ConnectionUtil.getConnection();
            StudentDao studentDao = new StudentDaoImpl(conn);
            gradeId = studentDao.selectGradeIdByStudentNo(studentNo);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionUtil.closeALL(null, null, conn);
        }
        return gradeId;
    }
}
